﻿using Authentication.Core.Configuration;
using Authentication.Model.Interface;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveListRequestCreateDaoByUserServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveListRequestCreateDaoByUserRequest, RetrieveListRequestCreateDaoByUserResponse>
    {

        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public string TextSearch;
        public FilterRecord Filter = null;
        private UserRecord currentUser;
        public RetrieveListRequestCreateDaoByUserServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveListRequestCreateDaoByUserRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (Request.CurrentUser == null)
            {
                throw new ArgumentException("Dao must be set", "request");
            }
            Filter = Request.Filter;
        }

        protected override bool OnExecute(RetrieveListRequestCreateDaoByUserResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            int pageSize = 0, pageIndex = 0, totalRecord = 0;
            var applicationKey = Request.Header.ApplicationKey;
            try
            {
                if (Request.Page != null)
                {
                    pageIndex = Request.Page.PageIndex ?? 0;
                    pageSize = Request.Page.PageSize ?? 0;
                }
                var listUser = Providers.RequestDao.RetrieveListCreateDaoRequest(out totalRecord, currentUser, Filter, pageSize, pageIndex, Request.Sort);
                if (listUser == null)
                {
                    response.ErrorMessage = string.Format("Can not found user with Dao={0}", currentUser.Dao);
                    response.IsSuccess = false;
                }
                List<RequestCreateDaoRecord> records = listUser.Select(x => new RequestCreateDaoRecord
                {
                    Pic = x.Pic,
                    FullName = x.FullName,
                    Cif = x.Cif,
                    Email = x.Email,
                    Phone = x.Phone,
                    LegalId = x.LegalId,
                    PathOfferLetter = x.LinkOfferLetter,
                    StaffId = x.StaffId,
                    StatusCreateDaoRequest = x.Status,
                    CreateRequestTime = x.CreatedDate,
                    PositionName = x.PositionName,
                    Zone = x.Branch == null ? string.Empty : x.Branch.Region,
                    Branch = x.Branch == null ? string.Empty : x.Branch.BranchNameSme,
                    RequestId = x.Id,
                    Dao = string.IsNullOrEmpty(x.Dao) ? string.Empty : x.Dao,
                    BranchId = x.Branch.BranchId,
                    RejectReason = string.IsNullOrEmpty(x.RejectReason) ? string.Empty : x.RejectReason,
                    RejectPerson = string.IsNullOrEmpty(x.RejectPerson) ? string.Empty : x.RejectPerson + " ",
                    NeedApproved = ((SystemConfiguration.PostionHeadBranch.ToUpper().Contains(currentUser.Position.ToUpper()) || currentUser.GroupIdentifier.Equals("2")) && x.Status == (int)StatusRequestCreateDaoEnum.Request && !string.IsNullOrEmpty(x.LinkOfferLetter)),
                    NeedUploadOfferLetter = string.IsNullOrEmpty(x.LinkOfferLetter) && x.Pic.Equals(currentUser.UserName) && x.Status == (int)StatusRequestCreateDaoEnum.Request,
                    CanReject = x.Status == (int)StatusRequestCreateDaoEnum.Request || ((x.Status == (int)StatusRequestCreateDaoEnum.BranchApproved || x.Status == (int)StatusRequestCreateDaoEnum.RegionApproved) && SystemConfiguration.UserBICApprovedDao.Contains(currentUser.UserName))
                }).ToList();
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord,
                };
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.CurrentUser.Dao);
                response.IsSuccess = false;
            }
            return isSuccess;
        }
    }
}